<?php
/**
 * Project     Newsletter for Joomla 1.5.x
 * Release     2.0.x
 * @package    Communicator
 * @subpackage Search Plugin
 * @version    $Id$
 * @author     Erik Damke (commjed@jocomail.de)
 * @link       http://joomlacode.org/gf/project/communicator/
 * @copyright  Copyright 2009 Erik Damke - All rights reserved
 * @license    http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * This program is free software; under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$mainframe->registerEvent( 'onSearch',      'plgSearchCommunicator' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchCommunicatorAreas' );

JPlugin::loadLanguage( 'plg_search_communicator' );

/**
 * @return array An array of search areas
 */
function &plgSearchCommunicatorAreas()
{
	static $areas = array(
		 'newsletter' => 'Newsletter'
	    );
	return $areas;
}

/**
 * Content Search method
 * The sql must return the following fields that are used in a common display
 * routine: href, title, section, created, text, browsernav
 * @param string Target search string
 * @param string mathcing option, exact|any|all
 * @param string ordering option, newest|oldest|popular|alpha|category
 * @param mixed An array if the search it to be restricted to areas, null if search all
 */

function plgSearchCommunicator( $text, $phrase='', $ordering='', $areas=null )
{
	global $mainframe;

	$db	=& JFactory::getDBO();
	$user	=& JFactory::getUser();
	$searchText = $text;

	require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_search'.DS.'helpers'.DS.'search.php');

	if (is_array( $areas )) {
		if (!array_intersect( $areas, array_keys( plgSearchCommunicatorAreas() ) )) {
			return array();
		}
	}

	// load plugin params info
   $plugin        =& JPluginHelper::getPlugin('search', 'communicator');
   $pluginParams  = new JParameter( $plugin->params );

   $limit         = $pluginParams->def( 'search_limit', 50 );
   $sHtmlContent  = $pluginParams->get( 'search_html_content', 1 );
   $sTextContent  = $pluginParams->get( 'search_text_content', 0 );

   $text = trim( $text );
   if ($text == '') {
      return array();
   }

   $where='';
   if ($phrase == 'exact')
      {
      $qtext  = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
      $wheres2    = array();
      $wheres2[]  = 'LOWER(subject) LIKE '.$qtext;
      if ($sHtmlContent) {
         $wheres2[]  = 'LOWER(html_message) LIKE '.$qtext;
      }
      if ($sTextContent) {
         $wheres2[]  = 'LOWER(message) LIKE '.$qtext;
      }
      $where = '(' . implode( ') OR (', $wheres2 ) . ')';
      }
   else
      {
      $words = explode( ' ', $text );
      $wheres = array();
      foreach ($words as $word) {
         $qword  = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
         $wheres2    = array();
         $wheres2[]  = 'LOWER(subject) LIKE '.$qword;
         if ($sHtmlContent) {
            $wheres2[]  = 'LOWER(html_message) LIKE '.$qword;
         }
         if ($sTextContent) {
            $wheres2[]  = 'LOWER(message) LIKE '.$qword;
         }
         $wheres[]   = implode( ' OR ', $wheres2 );
         }
      if ($phrase == 'all') {
         $separator = 'AND';
         }
      else {
         $separator = 'OR';
         }
      $where = '(' . implode( ") $separator (" , $wheres ) . ')';
      }

   switch ($ordering) {
      case 'oldest':
         $order = 'created ASC';
         break;
      case 'alpha':
         $order = 'subject ASC';
         break;
      case 'newest':
      default:
         $order = 'created DESC';
         break;
   }

   $section = JText::_( 'Newsletter' );
   $query = 'SELECT subject AS title, html_message AS text, created AS created, '.
            $db->Quote($section).' AS section, '.
            ' CONCAT(\'index.php?option=com_communicator&task=view&id=\', id) AS href,' .
            ' "2" AS browsernav' .
            ' FROM `#__communicator`' .
            ' WHERE '.$where.
            ' AND published = 1'.
            ' ORDER BY '.$order;

   $db->setQuery( $query, 0, $limit );
   $rows = $db->loadObjectList();
   return $rows;
}
?>
